import Preloader from "./preload"
import GameConst from "./GameConst";
import LevelManager from "./levelmanager";
import Match3Level from "./match3"
import SoundsManager from "./soundmanager"
import ObjectPool from "./objpool"
import MenuManager from "./menumanager"
import AnimationManager from "./amn/AnimationManager";
import Adapter from "../../../adapter/adapter";
import WxAdapter from "../../../adapter/adapter-wx";
import Utils from "./utils";
export default class App{
    constructor() {
        var t = this;
        this.atlases = [];
        this.musicPlayed = false;
        this.gameTime = 0;
        this.isPaused = false;
        this.timeSinceAds = 0;
        this.visibilityState = null;
        this.isPlayingOnMobile = true;
        this.isHidden = false;
        GameConst.game = this;
        if(Laya.MiniAdpter){
            GameConst.adapter = new WxAdapter();
        }else{
            GameConst.adapter = new Adapter();
        }
        GameConst.adapter.login();
        // this.stage = stage;
        // createjs.Touch.enable(this.stage, true);
        this.preloader = new Preloader;
        // setTimeout(function () {
        //     window.scrollTo(0, 1);
        // }, 1e3);
        // if (viewporter && viewporter.ACTIVE) {
        //     window.addEventListener("viewportready", function () {
        //         return t.resize();
        //     }, true);
        //     window.addEventListener("viewportchange", function () {
        //         return t.resize();
        //     }, true);
        // } else
        //     window.addEventListener("resize", function () {
        //         return t.resize();
        //     }, true);
        this.resize();
        // document.addEventListener("touchmove", touchHandlerDummy, false);
        // window.addEventListener("touchmove", touchHandlerDummy, false);
        // window.addEventListener("touchstart", function () {});
        // createjs.Ticker.setFPS(320);
        // createjs.Ticker.addEventListener("tick", function (e) {
        //     t.update(e);
        // });

        Laya.timer.frameLoop(1,this,this.update);
    }

    onLoadComplete() {
        if (GameConst.levelManager)
            return;
        this.preloader.destroy();
        this.preloader = null;
        this.timeSinceAds = 0;
        this.resize();
        GameConst.levelManager = new LevelManager;
        // var atlas = Laya.Loader.getAtlas("jelly.atlas");
        //atlas.images[0] = this.preloader.loader.getResult("abc.png");
        // spriteSheetInfo = atlas;
        // Utils.createAnimation("chocolate white destr", "chocolate white destr", 1, 18, 1, null, 50);
        // Utils.createAnimation("color bonus", "color bonus", 1, 37, 1, null, 50);
        // Utils.createAnimation("bonus blink", "color bonus", 30, 37, 1, null, 50);
        // Utils.createAnimation("chocolate destroy", "chocolate destroy", 1, 15, 1, null, 50);
        // Utils.createAnimation("color wall", "color wall", 1, 18, 1, null, 50);
        // Utils.createAnimation("bluex", "blue", 1, 16, 1, null, 50);
        // Utils.createAnimation("greenx", "green", 1, 13, 1, null, 50);
        // Utils.createAnimation("orangex", "orange", 1, 17, 1, null, 50);
        // Utils.createAnimation("purplex", "purple", 1, 16, 1, null, 50);
        // Utils.createAnimation("mine", "mine", 1, 15, 1, null, 50);
        // Utils.createAnimation("water_splash", "water_splash", 1, 14, 1, null, 50);
        // Utils.createAnimation("teleport_in", "teleport_in", 1, 11, 1, null, 50);
        // Utils.createAnimation("teleport_out", "teleport_out", 1, 11, 1, null, 50);
        // Utils.createAnimation("bush_anim", "bush_anim", 1, 19, 1, null, 50);
        // Utils.createAnimation("bush_anim_reverse", "bush_anim", 19, 1, 1, null, 50);
        // Utils.createFontFrames(atlas);
        // this.atlases.push(new createjs.SpriteSheet(atlas));
        GameConst.animationManager = new AnimationManager;
        // this.animationManager.putAnimation("logo", this.preloader.loader.getResult("logo text"), 0);
        // GameConst.animationManager.putAnimation("button",Laya.loader.getRes("button.txt"), 0);
        // this.animationManager.putAnimation("Char", this.preloader.loader.getResult("Char win"), 0);
        // this.animationManager.putAnimation("Char lose", this.preloader.loader.getResult("Char lose"), 0);
        // this.animationManager.putAnimation("gloss anim", this.preloader.loader.getResult("gloss anim"), 0);
        // GameConst.animationManager.putAnimation("pointer", Laya.Loader.getRes("pointer.txt"), 0);
        // GameConst.animationManager.putAnimation("buttons anim", Laya.loader.getRes("buttons pause anim.txt"), 0);
        // atlas = this.preloader.loader.getResult("font2Json");
        // atlas.images[0] = this.preloader.loader.getResult("font2.png");
        // createFontFrames(atlas);
        // this.atlases.push(new createjs.SpriteSheet(atlas));
        // atlas = this.preloader.loader.getResult("artEpisodeJson");
        // atlas.images[0] = this.preloader.loader.getResult("artEpisode");
        // this.atlases.push(new createjs.SpriteSheet(atlas));
        if (!GameConst.pool){
            GameConst.pool = new ObjectPool;
        }
        Match3Level.instance = new Match3Level;
        this.soundManager = new SoundsManager;
        
        this.menuManager = new MenuManager;        

        this.resize();
        // LevelManager.instance.prepareToLoadLevel(1);
        // this.setVisibilityListener();
        Laya.stage.on(Laya.Event.MOUSE_DOWN,this,this.onDown);
        Laya.stage.on(Laya.Event.MOUSE_UP,this,this.onUp);

        var test = Utils.getQueryString('testlvl',0);
        if(test>0){
            GameConst.levelManager.loadLevel(test);       
        }else{
            this.menuManager.show(this.menuManager.map, true);
        }
    }

    loadApi () {
        this.onLoadComplete();
    };

    pauseGame () {
        this.isPaused = true;
        SoundsManager.instance.pauseMusic();
        this.timeSinceAds = 0;
    };
    resumeGame() {
        this.isPaused = false;
        SoundsManager.instance.resumeMusic();
        this.timeSinceAds = 0;
    };
    
    update () {
        if (this.isPaused)
            return;
        var delta = Laya.timer.delta;
        //this.stage.update(t);
        if (GameConst.level && GameConst.level.isActive)
            GameConst.level.update(delta / 1e3);
        if (this.menuManager)
            this.menuManager.update(delta / 1e3);
        if (this.preloader)
            this.preloader.update(delta / 1e3);
        if (this.soundManager)
            this.soundManager.update(delta / 1e3);
        this.gameTime += delta / 1e3;
        this.timeSinceAds += delta / 1e3;
    };
    // e.prototype.onPreloaderDown = function (e) {
    //     this.preloader.onDown(e.stageX, e.stageY);
    //     e.nativeEvent && e.preventDefault && e.preventDefault() && e.stopPropagation();
    // };
    onDown(evt) {
        if (this.isPaused)
            return;
        if(!this.soundManager){
            return;
        }

        var menu = MenuManager.instance.current;
        if (menu){            
            menu.onDown(evt.stageX, evt.stageY);
            return;
        }
        if(this.menuManager.target.isMenuActive){
            this.menuManager.target.onUp(evt.stageX,evt.stageY);
            return;
        }
        var isTutorial = false;//menu == Tutorial.instance && !Tutorial.instance.tapAllowed;
        if (GameConst.level && !isTutorial)
            GameConst.level.onMouseDown(evt.stageX, evt.stageY);
        else if (isTutorial)
            GameConst.level.onHudDown(evt.stageX, evt.stageY);
        evt.nativeEvent && evt.preventDefault && evt.preventDefault() && evt.stopPropagation();
    };
    onUp (evt) {
        if (this.isPaused)
            return;
        this.soundManager.playMusic();
        if(this.menuManager.target.isMenuActive){
            this.menuManager.target.onUp(evt.stageX,evt.stageY);
            return;
        }
        var menu = MenuManager.instance.current;
        if (menu)
            menu.onUp(evt.stageX, evt.stageY);
        evt.nativeEvent && evt.preventDefault && evt.preventDefault() && evt.stopPropagation();
    };
    resize() {
        if (this.menuManager)
            this.menuManager.onResize();
    };

    onVisibilityChange () {
        var e = this.visibilityState && document[this.visibilityState] && document[this.visibilityState] == "hidden";
        this.isHidden = e;
        if (SoundsManager.instance.musicPaused && !e)
            SoundsManager.instance.resumeMusic();
        else if (e && !SoundsManager.instance.musicPaused)
            SoundsManager.instance.pauseMusic();
    };
}

    


